<template>
  <div class="menu-bar">
    <h1 class="logo">STW<span>接单平台</span></h1>

    <ul>
      <li>
        <p @click="onHome">首页</p>
      </li>
      <li>
        <p @click="onOrder">订单列表</p>
      </li>
      <li>
        <p @click="onChat" :class="{ 'red-point': unreadMessagesCount > 0 }">
          聊天消息
        </p>
      </li>
      <li>
        <p @click="onUser">个人中心</p>
      </li>
      <li><p @click="onUs">关于我们</p></li>
      <li><p @click="onReport">投诉举报</p></li>
    </ul>
  </div>
</template>
<script>
import router from "@/router";
import { ElNotification } from "element-plus";

export default {
  data() {
    return {
      userid: "", // 当前页面的用户id
      unreadMessagesCount: 0,
    };
  },
  mounted() {
    const params = new URLSearchParams();
    this.axios.post("http://127.0.0.1:8000/get_userid/", params).then((res) => {
      if (res.data.success === 1) {
        this.userid = res.data.userid;
        // this.getMsg(); // 当前用户登录了，获取是否有新消息
      }
    });
  },

  methods: {
    // 消息通知
    // getMsg() {
    //   // 使用长轮询
    //   var xhr = new XMLHttpRequest();
    //   xhr.open(
    //     "GET",
    //     `http://127.0.0.1:8000/msg_poll2/?userid=${this.userid}`,
    //     true
    //   );

    //   xhr.onreadystatechange = () => {
    //     if (xhr.readyState == 4 && xhr.status == 200) {
    //       var data = JSON.parse(xhr.responseText);
    //       // 处理从服务器接收到的数据，例如更新UI
    //       let temp = 0;
    //       for (const key in data) {
    //         temp += data[key].message_count;
    //       }
    //       this.unreadMessagesCount = temp;
    //       // console.log("top_nav Received data", this.unreadMessagesCount);
    //       // 递归调用以启动下一个长轮询
    //       this.getMsg();
    //     } else if (xhr.readyState == 4) {
    //       // 处理其他状态码或错误
    //       console.error("Error during long polling:", xhr.status);

    //       // 递归调用以启动下一个长轮询
    //       this.getMsg();
    //     }
    //   };

    //   xhr.send();
    // },

    onHome() {
      this.$router.push({
        path: "/",
      });
    },
    onOrder() {
      this.$router.push({
        path: "/orderPage",
      });
    },
    onChat() {
      this.axios.post("http://127.0.0.1:8000/get_userid/").then((res) => {
        if (res.data.success) {
          this.$router.push({
            path: "/chatPage/" + -res.data.userid,
          });
        } else {
          this.is_login = false;
          ElNotification({
            title: "未登录",
            message: "您暂未登录，请登录后再试",
            type: "error",
          });
          router.push({ name: "home" });
        }
      });
    },
    onUser() {
      this.$router.push({
        path: "/UserPage",
      });
    },
    onUs() {
      this.$router.push({
        path: "/Us",
      });
    },
    onReport() {
      this.$router.push({
        path: "/Report",
      });
    },
  },
};
</script>
<style>
:root {
  --color-primary: rgb(170, 113, 207);
  --color-white: #e9e9e9;
  --color-black: #141d28;
  --color-black-1: #212b38;
}

* {
  margin: 0;
  padding: 0;
  /*标准盒子*/
  box-sizing: border-box;
}

.badge {
  background-color: red;
  color: white;
  padding: 5px;
  border-radius: 50%;
  margin-left: 5px;
}

.logo {
  color: var(--color-white);
  font-size: 25px;
}

.logo span {
  color: rgb(170, 113, 207);
  /* color: var(--color-primary); */
}

.menu-bar {
  background-color: #282b2c;
  height: 10vh;
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 2% 0 5%;
  position: relative;
}

.menu-bar ul {
  list-style: none;
  display: flex;
}

.menu-bar ul li {
  width: 120px;
  padding: 10px 20px;
  text-align: center;
  position: relative;
}

.menu-bar ul li p {
  font-size: 17px;
  color: var(--color-white);
  text-decoration: none;
  transition: all 0.3s;
}

.menu-bar ul li p:hover {
  color: var(--color-primary);
  cursor: pointer;
}

.red-point {
  position: relative;
}

.red-point::before {
  content: " ";
  border: 4.5px solid red; /*设置红色*/
  border-radius: 4.5px; /*设置圆角*/
  position: absolute;
  z-index: 1000;
  right: 0;
  margin-right: -8px;
}
</style>
